home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacsbugBook / MacsBug Book Disk / Sample Application Sources / Chapter 4 App / Chapter 4 App.c next >
Encoding:
C/C++ Source or Header  |  1991-02-23  |  3.8 KB  |  201 lines  |  [TEXT/KAHL]

  1. /*
  2.   * Chapter 4 Sample Application
  3.   *
  4.   */
  5.  
  6.  /************************************
  7. * Inclusions
  8. ************************************/
  9. #ifdef THINK_C
  10.     #include <ColorToolbox.h>
  11.     #include <DialogMgr.h>
  12.     #include <ResourceMgr.h>
  13.     #include <EventMgr.h>
  14.     #include <MenuMgr.h>
  15. #endif
  16.  
  17. #ifdef applec
  18.     #include <QuickDraw.h>
  19.     #include <Resources.h>
  20.     #include <Windows.h>
  21.     #include <Dialogs.h>
  22.     #include <Events.h>
  23.     #include <OSEvents.h>
  24.     #include <Menus.h>
  25.     #include <Memory.h>
  26.     #include <SegLoad.h>
  27.     #include <Desk.h>
  28.     #include <Fonts.h>
  29.  
  30.     #define thePort qd.thePort
  31. #endif
  32.  
  33. #include "BigEasy2.h"
  34.  
  35. /************************************
  36. * Types and globals
  37. ************************************/
  38. Rect         dOffBounds = {0,0,200,200};
  39.  
  40. DrawSplug()
  41. /*
  42.  * Draws the window.
  43.  */
  44. {
  45.     EraseRect(&gBigRect);
  46.     MoveTo(20,20);
  47.     DrawString("\pUse Memory Menu.");
  48.  
  49. }
  50.  
  51. ClickSplug(n,p)
  52. /*
  53.  * Come here for a click in the window.
  54.  */
  55. short n;            /* window number        */
  56. Point p;            /* where clicked        */
  57. {
  58. #ifdef applec
  59.     #pragma unused (n,p);
  60. #endif
  61. }
  62.  
  63. GoAwaySplug(n)
  64. /*
  65.  * Close that window...
  66.  */
  67. short n;
  68. {
  69.     UninstallWindow(n);
  70. }
  71.  
  72. void ActivateSplug(n)
  73. {
  74. #ifdef applec
  75.     #pragma unused (n);
  76. #endif
  77.     SetMenuItem(23,true,0,0,nil);                /* enable "Close" menu item        */
  78.     SetMenuItem(24,false,0,0,nil);                /* disable "Open" menu item        */
  79.     KillEds();
  80. }
  81.  
  82. void DeactivateSplug(n)
  83. {
  84. #ifdef applec
  85.     #pragma unused (n);
  86. #endif
  87.     SetMenuItem(23,false,0,0,nil);                /* disable "Close" menu item        */
  88.     SetMenuItem(24,true,0,0,nil);                /* enable "Open" menu item        */
  89. }
  90.  
  91. void pnull(){}
  92. letsquit()
  93. {
  94.     gQuitApp++;
  95. }
  96.  
  97. KillEds()
  98. {
  99.     EnDisEdits(0,0,0,0,0);
  100. }
  101.  
  102. OpenWindow()
  103. {
  104. Rect r;
  105.  
  106.     SetRect(&r,100,100,100+dOffBounds.right,100+dOffBounds.bottom);
  107.     
  108.     InstallWindow(1,"\pWindow",&r,0,1,
  109.             (FP)DrawSplug,(FP)ClickSplug,(FP)pnull,(FP)GoAwaySplug,
  110.             (FP)ActivateSplug,(FP)DeactivateSplug,(FP)pnull, false);
  111. }
  112.  
  113. CorruptHeap()
  114. {
  115. long        size=0x100;
  116. short    iii;
  117. Handle    myHandle;
  118.  
  119.     if( PutUpMessage( 1, "\pThe heap becomes corrupted.  Use HD to find corrupted block.  Use ES to continue when Mac crashes." ) )
  120.     {
  121.         DebugStr("\pAllocates a handle and fills it with 5 ';hc;g" );
  122.         myHandle = NewHandle( size );
  123.         for( iii = 0; iii <=size; iii++ )
  124.             ((short *)*myHandle)[iii] = (short) 5;
  125.             
  126.         DebugStr("\pHeap is corrupted!  Use HD to find corrupted block, ES to continue ';hc;g" );
  127.         DisposHandle( myHandle );
  128.     }
  129. }
  130.  
  131. FragmentHeap()
  132. {
  133. long        size;
  134. long        memAvail = MaxMem( &size );
  135. Ptr        myPtr1;
  136. Ptr        myPtr2;
  137. Handle    myHandle;
  138.  
  139.     if( PutUpMessage( 1, "\pNewHandle will fail due to a fragmented heap. " ) )
  140.     {
  141.         if( memAvail + size > 8000 )
  142.         {
  143.             myPtr1 = NewPtr( (memAvail+size)/2 );
  144.             myPtr2 = NewPtr( 4000 );                    /* ptr2 is now in the middle of the heap    */
  145.             DisposPtr( myPtr1 );
  146.             
  147.             DebugStr("\pNext NewHandle fails even though there is enough memory in heap" );
  148.             if( !(myHandle = NewHandle( (memAvail+size + 500)/2 ) ) )
  149.                 DebugStr("\pMemory allocation failed due to fragmented heap.  Use HD to see heap." );
  150.             else
  151.                 DisposHandle( myHandle );
  152.                 
  153.             DisposPtr( myPtr2 );
  154.         }
  155.     }
  156. }    
  157.  
  158. MemoryLeak()
  159. {
  160. Handle    myHandle;
  161.  
  162.     if( PutUpMessage( 1, "\pThis causes some memory to be leaked, eventually filling memory with allocated but unused blocks." ) )
  163.     {
  164.         myHandle = NewHandle( 4000 );
  165.     }
  166. }
  167.     
  168. InitVars()
  169. /*
  170.  * Called once at startup: yes, it
  171.  * inits the vars.
  172.  */
  173. {
  174. }
  175.  
  176. void Bootstrap()
  177. {
  178.     InitVars();
  179.  
  180. /*** Menu 2 ***/
  181.     InstallMenu("\pFile",(FP)pnull,0);
  182.     InstallItem("\pOpen/O",(FP)OpenWindow,23);
  183.     InstallItem("\pClose/W",(FP)GoAwaySplug,-24);
  184.     InstallItem("\p(-",(FP)pnull,0);
  185.     InstallItem("\pQuit/Q",(FP)letsquit,0);
  186.  
  187. /*** Menu 3 ***/
  188.     InstallEditMenu((FP)pnull,(FP)pnull,(FP)pnull,(FP)pnull,(FP)pnull);
  189.  
  190. /*** Menu 4 ***/
  191.     InstallMenu("\pMemory",(FP)pnull,0);
  192.     InstallItem("\pCorrupt Heap",(FP)CorruptHeap,25);
  193.     InstallItem("\pFragment Heap",(FP)FragmentHeap,29);
  194.     InstallItem("\pLeak Some Memory",(FP)MemoryLeak,26);
  195.     
  196.     KillEds();
  197. }
  198.  
  199.  
  200.  
  201.